package com.zyj.java;

/**
 * @program: JVMDemo
 * @ClassName DeadThreadTest
 * @author: YaJun
 * @Date: 2021 - 12 - 02 - 10:27
 * @Package: com.zyj.java
 * @Description:
 */

/**
 * 类加载时，天然的线程安全
 * JVM保证了一个类的<clinit>()方法在多线程下被同步加锁
 */
public class DeadThreadTest {



    public static void main(String[] args) {
        Runnable r = () -> {
            System.out.println(Thread.currentThread().getName() + "开始");
            DeadThread deadThread = new DeadThread();
            System.out.println(Thread.currentThread().getName() + "结束");
        };


        Thread t1 = new Thread(r, "线程一");
        Thread t2 = new Thread(r, "线程二");

        t1.start();
        t2.start();
    }


    static class DeadThread {
         static {
            if (true) {
                System.out.println(Thread.currentThread().getName() + "初始化当前类");
                while (true) {

                }
            }

        }
    }

}
